﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 必须是某个值, 示例:
/// <code>
/// [VMustBe(18)]
/// public int? Age{ get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustBeAttribute(object value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; internal set; } = value;
    public bool IgnoreCase { get; internal set; } = ignoreCase;
    public string ErrorMessage { get; internal set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustBeStringInternal(ctx, Value?.ToString(), IgnoreCase, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则必须是某个值, 示例:
/// <code>
/// [VMustBeIfNotNull(18)]
/// public int? Age{ get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustBeIfNotNullAttribute(object value, bool ignoreCase = false, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; internal set; } = value;
    public bool IgnoreCase { get; internal set; } = ignoreCase;
    public string ErrorMessage { get; internal set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustBeStringInternal(ctx, Value?.ToString(), IgnoreCase, ErrorMessage);
    }
}
